package com.zz.spring6.validator.one;


import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class PersonValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return Person.class.equals(aClass);
    }

    // 校验规则
    @Override
    public void validate(Object o, Errors errors) {
        // name 不能为空
        ValidationUtils.rejectIfEmpty(errors,"name",
                "name.empty","name is null");
        // age 不能小于0  不能大于200
        Person p = (Person) o;
        if (p.getAge()<0){
            errors.rejectValue("age",
                    "age.value.error","age < 0 ");
        } else if (p.getAge() > 200){
            errors.rejectValue("age",
                    "age.value.error.old","age > 200 ");
        }
    }
}
